package com.recordhistory.controller;

import com.recordhistory.enums.ResultEnum;
import com.recordhistory.model.EventOccur;
import com.recordhistory.model.HistoryImg;
import com.recordhistory.model.HistoryMain;
import com.recordhistory.model.ResultJson;
import com.recordhistory.service.EventOccurService;
import com.recordhistory.service.HistoryImgService;
import com.recordhistory.service.HistoryMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author ：LX
 * 创建时间： 2019/6/13. 10:00
 * 地点：广州
 * 目的: 事件
 * 备注说明：
 */
@Controller
@RequestMapping("/event")
public class EventOccurController {


    @Autowired
    private EventOccurService eventOccurService;
    @Autowired
    private HistoryMainService historyMainService;
    @Autowired
    private HistoryImgService historyImgService;


    /**
     * 跳转到 新增事件
     * @return
     */
    @RequestMapping("/toAddEventOccur")
    public String toAddEventOccur(ModelMap modelMap){
        List<HistoryMain> historyMainList = historyMainService.selectAllAdSort();
        modelMap.put("historyMainList", historyMainList);

        return "addEventOccur";
    }

    /**
     * 新增 事件
     * @param eventOccur 事件data
     */
    @RequestMapping("/addEventOccur")
    @ResponseBody
    public ResultJson addEventOccur(EventOccur eventOccur){
        eventOccurService.addEventOccur(eventOccur);

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "添加成功", null);
    }

    /**
     * 更新 事件
     * @param eventOccur 事件data
     */
    @RequestMapping("/updEventOccur")
    @ResponseBody
    public ResultJson updEventOccur(EventOccur eventOccur){
        eventOccurService.updEventOccur(eventOccur);

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "更新成功", null);
    }

    /**
     * 跳转到 更新事件页面
     * @param id 事件ID
     * @return
     */
    @RequestMapping("/toUpdEventOccur")
    public String toUpdEventOccur(String id, ModelMap modelMap){
        EventOccur eventOccur = eventOccurService.selectById(id);
        if (eventOccur != null){
            modelMap.put("eventOccur", eventOccur);
        }

        List<HistoryMain> historyMainList = historyMainService.selectAllAdSort();
        modelMap.put("historyMainList", historyMainList);

        List<HistoryImg> imgList =  historyImgService.selectByEventId(id);
        if (imgList != null && imgList.size() > 0){
            modelMap.put("imgList", imgList);
        }

        return "updEventOccur";
    }

    /**
     * 根据主键删除 事件
     * @param id 事件ID
     */
    @RequestMapping("/delEventOccur")
    @ResponseBody
    public ResultJson delEventOccur(String id){
        eventOccurService.delEventOccur(id);

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "删除成功", null);
    }
}
